package com.offcn.dao;

import com.offcn.entity.Page;
import com.offcn.entity.Student;

import java.util.List;

public class StudentDao extends BaseDao<Student> {
    //删除学生信息
    public boolean delete(int id){
        String sql = "delete from stu where sid=?";
        return update(sql,id)>0 ? true : false;
    }

    //添加学生信息
    public boolean insert(Student s){
        String sql = "insert into stu(name,age,tel,bir,sex,spic)values(?,?,?,?,?,?)";
        Object[] param={s.getName(),s.getAge(),s.getTel(),s.getBir(),s.getSex(),s.getSpic()};
        return update(sql,param)>0 ? true : false;
    }

    //根据id查询学生信息
    public Student stuSinSer(int id){
        String sql = "select * from stu where sid=?";
        return queryOne(sql,Student.class,id);
    }

    //修改学生信息
    public boolean updateStu(Student s,int id){
        String sql = "update stu set name=?,age=?,tel=?,bir=?,sex=?,spic=?where sid=?";
        Object[] param={s.getName(),s.getAge(),s.getTel(),s.getBir(),s.getSex(),s.getSpic(),id};
        return update(sql,param)>0 ? true : false;
    }

    //模糊查询
    public List<Student> StuLike(String name,String sex,Page p){
        StringBuffer sql = new StringBuffer("select * from stu where 1=1");
        if(name!=null&&!"".equals(name)){
            sql.append(" and name like '%"+name+"%'");
        }
        if("0".equals(sex)||"1".equals(sex)){
            sql.append(" and sex="+sex);
        }
        sql.append(" limit ?,?");
        return queryMore(sql.toString(),Student.class,p.getStartIndex(),p.getPageSize());
    }

    public int LikeNum(String name,String sex){
        StringBuffer sql = new StringBuffer("select count(*) from stu where 1=1");
        if(name!=null&&!"".equals(name)){
            sql.append(" and name like '%"+name+"%'");
        }
        if("0".equals(sex)||"1".equals(sex)){
            sql.append(" and sex="+sex);
        }
        return queryScar(sql.toString()).intValue();
    }

}
